﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Report
{
    public partial class ByBD : System.Web.UI.Page
    {
        private string asbOrLucent;
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.divError_Quarterly.InnerHtml = "";
            this.divError.InnerHtml = "";

            #region Get Entity Parameter

            if (Request.QueryString["ASBorLucent"] == null)
            {
                this.asbOrLucent = "";
            }
            else
            {
                this.asbOrLucent = Request.QueryString["ASBorLucent"];
            }

            #endregion

            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);

            #region Check if user has entity permission, and close date is defined
            if (!ReportHelper.CheckEntityPermission(user.EmployeeId,this.asbOrLucent))
            {
                this.pnlContent.Visible = false;
                this.divReport.InnerHtml = "";
                divError.InnerHtml = AlertMessage.NoEntityPermission;
                return;
            }
            if (!ReportHelper.IsCloseDateSet())
            {
                this.pnlContent.Visible = false;
                this.divReport.InnerHtml = "";
                divError.InnerHtml = AlertMessage.CloseDateNoSet;
                return;            
            }
            #endregion

            if (!Page.IsPostBack)
            {
                this.FillInReportDropdown();
                this.FillInReportDropdown_Quarterly();
                this.FillInReportDropdown_Adjust();

                //attach client events
                this.btnSave.Attributes.Add("onClick", "RptSaveToExcel('" + ReportHelper.ReportType.ByBD.ToString()+ "');return false;");
                this.btnExport.Attributes.Add("onClick", "RptExportToExcel('" + ReportHelper.ReportType.ByBD.ToString() + "');return false;");
                this.drRpt.Attributes.Add("onchange", "RptDownloadToExcel(this);return false;");


                //Get Default month value;
                this.txtNoOfMonth.Text = ReportHelper.GetReportDefaultMonth().ToString();
                this.txtReportDate.Text = System.DateTime.Now.ToString("yyyy-MM-dd");

                this.btnExport.Visible = false;
                this.btnSave.Visible = false;
                this.txtReportDate.Visible = false;
                this.DateIcon01.Visible = false;
                this.drRpt.Visible = false;
                this.lblRptDate.Visible = false;

                this.btnExport_Quarterly.Attributes.Add("onClick", "RptExportToExcel('" + ReportHelper.ReportType.ByBD_Quarterly.ToString() + "');;return false;");
                this.btnSave_Quarterly.Attributes.Add("onClick", "RptSaveToExcel('" + ReportHelper.ReportType.ByBD_Quarterly.ToString() + "');return false;");
                this.drRpt_Quarterly.Attributes.Add("onchange", "RptDownloadToExcel(this);return false;");
                this.txtReportDate_Quarterly.Text = System.DateTime.Now.ToString("yyyy-MM-dd");

                this.btnExport_Quarterly.Visible = false;
                this.btnSave_Quarterly.Visible = false;
                this.DateIcon01_Quarterly.Visible = false;
                this.drRpt_Quarterly.Visible = false;
                this.lblRptDate_Quarterly.Visible = false;

                #region  if user is admin or has download permission, then show dropdown list for downloading saved reports
                if (this.asbOrLucent == "" || this.asbOrLucent == "2")
                {
                    if (ReportHelper.IsAdmin(user.EmployeeId) || ReportHelper.AllowedDownload(user.EmployeeId))
                    {
                         this.drRpt.Visible=true;
                         this.drRpt_Quarterly.Visible = true;
                    }
                }               
                #endregion                

                //Run Report
                RunReport(System.Convert.ToInt32(this.txtNoOfMonth.Text.ToString()), this.asbOrLucent, user.EmployeeId, -1);
                RunReport_Quarterly(System.Convert.ToInt32(this.txtNoOfMonth.Text.ToString()), this.asbOrLucent, user.EmployeeId, int.Parse(ddlAdjust.SelectedItem.Value));
                
            }
        }
  
        protected void btnRun_Click(object sender, EventArgs e)
        {
            RunReport(System.Convert.ToInt32(this.txtNoOfMonth.Text.ToString()),this.asbOrLucent,user.EmployeeId,-1);
            //RunReport_Quarterly(System.Convert.ToInt32(this.txtNoOfMonth.Text.ToString()), this.asbOrLucent, user.EmployeeId, int.Parse(ddlAdjust.SelectedItem.Value));
        }

        protected void btnRunQuarter_Click(object sender, EventArgs e)
        {
            //RunReport(System.Convert.ToInt32(this.txtNoOfMonth.Text.ToString()), this.asbOrLucent, user.EmployeeId, int.Parse(ddlAdjust.SelectedItem.Value));
            RunReport_Quarterly(System.Convert.ToInt32(this.txtNoOfMonth.Text.ToString()), this.asbOrLucent, user.EmployeeId, int.Parse(ddlAdjust.SelectedItem.Value));
        }

        private void RunReport(int noOfMonth, string asbOrLucent,long employeeID,int adjust)
        {
            this.btnExport.Visible = false;
            this.btnSave.Visible = false;
            this.txtReportDate.Visible = false;
            this.DateIcon01.Visible = false;

            string err = string.Empty;
            string reportHTML = string.Empty;
            string excelHTML = string.Empty;
            try
            {
                string rptTitle = string.Empty;
                reportHTML = BEGDC.PreContract.BLL.ReportHelper.GetReportHTML2DimByDate(employeeID,
                    ReportHelper.ReportType.ByBD,
                    asbOrLucent,
                    noOfMonth,
                    adjust,
                    out err,
                    out excelHTML,
                    out rptTitle);
                reportHTML = rptTitle + reportHTML;

                if (err != string.Empty)
                {
                    this.divReport.InnerHtml = "";
                    this.divError.InnerHtml = err;
                    this.btnExport.Visible = false;
                    this.btnSave.Visible = false;
                    this.txtReportDate.Visible = false;
                    this.DateIcon01.Visible = false;
                    this.lblRptDate.Visible = false;
                }
                else
                {
                    this.divReport.InnerHtml = reportHTML;
                    Session[ReportHelper.ReportType.ByBD.ToString()] = excelHTML;
                    this.divError.InnerHtml = "";
                    this.btnExport.Visible = true;
                    if (ReportHelper.IsAdmin(employeeID) && (this.asbOrLucent == "" || this.asbOrLucent == "2"))
                    {
                        this.btnSave.Visible = true;
                        this.txtReportDate.Visible = true;
                        this.DateIcon01.Visible = true;
                        this.lblRptDate.Visible = true;
                    }

                }
            }
            catch
            {
                this.divReport.InnerHtml = "";
                this.btnExport.Visible = false;
                this.btnSave.Visible = false;
                this.txtReportDate.Visible = false;
                this.DateIcon01.Visible = false;
                divError.InnerHtml = AlertMessage.GetReportDataError;
                //this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + AlertMessage.GetReportDataError  + "')</script>");

            }

        }

        private void RunReport_Quarterly(int noOfMonth, string asbOrLucent, long employeeID,int adjust)
        {
            this.btnExport_Quarterly.Visible = false;
            this.btnSave_Quarterly.Visible = false;
            this.txtReportDate_Quarterly.Visible = false;
            this.DateIcon01_Quarterly.Visible = false;
            string err = string.Empty;
            string reportHTML = string.Empty;
            string excelHTML = string.Empty;
            try
            {
                string rptTitle = string.Empty;
                reportHTML = BEGDC.PreContract.BLL.ReportHelper.GetReportHTML2DimByDate(employeeID,
                    ReportHelper.ReportType.ByBD_Quarterly,
                    asbOrLucent,
                    noOfMonth,
                    adjust,
                    out err,
                    out excelHTML,
                    out rptTitle);
 

                if (err != string.Empty)
                {
                    this.LabelReport_Quarterly.Text = "";
                    this.divError_Quarterly.InnerHtml = err;
                    this.btnExport_Quarterly.Visible = false;
                    this.btnSave_Quarterly.Visible = false;
                    this.txtReportDate_Quarterly.Visible = false;
                    this.DateIcon01_Quarterly.Visible = false;
                    this.lblRptDate_Quarterly.Visible = false;
                }
                else
                {
                    this.LabelReport_Quarterly.Text = rptTitle;
                    ScrollReport1.ContentHtml = reportHTML;
                    ScrollReport1.InitControl();
                    Session[ReportHelper.ReportType.ByBD_Quarterly.ToString()] = excelHTML;
                    this.divError_Quarterly.InnerHtml = "";

                    this.btnExport_Quarterly.Visible = true;
                    if (ReportHelper.IsAdmin(employeeID) && (this.asbOrLucent == "" || this.asbOrLucent == "2"))
                    {
                        this.btnSave_Quarterly.Visible = true;
                        this.txtReportDate_Quarterly.Visible = true;
                        this.DateIcon01_Quarterly.Visible = true;
                        this.lblRptDate_Quarterly.Visible = true;
                    }

                }
            }
            catch
            {
                this.LabelReport_Quarterly.Text = "";
                this.btnExport.Visible = false;
                this.btnSave_Quarterly.Visible = false;
                this.txtReportDate_Quarterly.Visible = false;
                this.DateIcon01_Quarterly.Visible = false;
                divError_Quarterly.InnerHtml = AlertMessage.GetReportDataError;
                //this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + AlertMessage.GetReportDataError  + "')</script>");

            }
        }

        private void FillInReportDropdown()
        {
            DataTable dtReport=ReportHelper.GetReportHistory(ReportHelper.ReportType.ByBD);
            this.drRpt.Items.Clear();
            ListItem lstItem;
            lstItem = new ListItem();
            lstItem.Value = "0";
            lstItem.Text = AlertMessage.DownloadReport;
            this.drRpt.Items.Add(lstItem);
            for (int i=0;i<dtReport.Rows.Count;i++)
            {   lstItem=new ListItem();
                lstItem.Value=dtReport.Rows[i]["ReportID"].ToString();
                lstItem.Text = System.Convert.ToDateTime(dtReport.Rows[i]["ReportDate"].ToString()).ToString("yyyy-MM-dd");
                this.drRpt.Items.Add(lstItem);
            }
        }

        private void FillInReportDropdown_Quarterly()
        {
            DataTable dtReport = ReportHelper.GetReportHistory(ReportHelper.ReportType.ByBD_Quarterly);
            this.drRpt_Quarterly.Items.Clear();
            ListItem lstItem;
            lstItem = new ListItem();
            lstItem.Value = "0";
            lstItem.Text = AlertMessage.DownloadReport;
            this.drRpt_Quarterly.Items.Add(lstItem);
            for (int i = 0; i < dtReport.Rows.Count; i++)
            {
                lstItem = new ListItem();
                lstItem.Value = dtReport.Rows[i]["ReportID"].ToString();
                lstItem.Text = System.Convert.ToDateTime(dtReport.Rows[i]["ReportDate"].ToString()).ToString("yyyy-MM-dd");
                this.drRpt_Quarterly.Items.Add(lstItem);
            }
        }

        private void FillInReportDropdown_Adjust()
        {
            BaseFunction.BindContractAdjustDropDownList(ddlAdjust, string.Empty, true);
        }
    }
}
